/// 🎯 等级识别工具类
/// 统一管理用户等级计算逻辑
/// 
/// 等级规则：
/// - >= 3000: 玩神 (level 4)
/// - >= 1200: 大玩家 (level 3)
/// - >= 600:  资深玩家 (level 2)
/// - >= 300:  会玩儿 (level 1)
/// - < 300:   刚玩儿 (level 0)
class LevelUtil {
  /// 根据积分计算等级
  /// @param points - 用户积分
  /// @returns 等级编号 (0-4)
  static int calculateLevelFromPoints(int points) {
    if (points < 0) {
      return 0; // 刚玩儿
    }
    
    if (points >= 3000) {
      return 4; // 玩神
    } else if (points >= 1200) {
      return 3; // 大玩家
    } else if (points >= 600) {
      return 2; // 资深玩家
    } else if (points >= 300) {
      return 1; // 会玩儿
    } else {
      return 0; // 刚玩儿
    }
  }

  /// 根据积分返回等级名称
  /// @param points - 用户积分
  /// @returns 等级名称
  static String getLevelNameFromPoints(int points) {
    final level = calculateLevelFromPoints(points);
    
    switch (level) {
      case 0:
        return '刚玩儿';
      case 1:
        return '会玩儿';
      case 2:
        return '资深玩家';
      case 3:
        return '大玩家';
      case 4:
        return '玩神';
      default:
        return '刚玩儿';
    }
  }

  /// 获取所有等级配置
  /// @returns 等级配置数组
  static List<LevelConfig> getAllLevels() {
    return [
      LevelConfig(id: 1, name: '刚玩儿', points: '0~299', minPoints: 0),
      LevelConfig(id: 2, name: '会玩儿', points: '300~599', minPoints: 300),
      LevelConfig(id: 3, name: '资深玩家', points: '600~1199', minPoints: 600),
      LevelConfig(id: 4, name: '大玩家', points: '1200~2999', minPoints: 1200),
      LevelConfig(id: 5, name: '玩神', points: '3000+', minPoints: 3000),
    ];
  }

  /// 获取等级特权描述
  /// @param levelName - 等级名称
  /// @returns 特权描述
  static String getLevelPrivilege(String levelName) {
    const privileges = {
      '刚玩儿': '无',
      '会玩儿': '好友申请',
      '资深玩家': '免费好友申请3次/天+免费发布活动1次/天',
      '大玩家': '免费好友申请5次/天+免费发布活动2次/天',
      '玩神': '专属头像框+免费好友申请10次/天+免费发布活动5次/天',
    };
    
    return privileges[levelName] ?? '无';
  }

  /// 根据积分获取等级进度信息
  /// @param points - 当前积分
  /// @returns LevelProgress对象
  static LevelProgress getLevelProgress(int points) {
    final currentLevel = calculateLevelFromPoints(points);
    final currentLevelName = getLevelNameFromPoints(points);
    final levels = getAllLevels();
    
    // 查找下一个等级
    final nextLevelConfig = levels.firstWhere(
      (level) => level.minPoints > points,
      orElse: () => levels.last,
    );
    
    // 已经是最高等级
    if (nextLevelConfig.minPoints <= points && points >= 3000) {
      return LevelProgress(
        currentLevel: currentLevel,
        currentLevelName: currentLevelName,
        nextLevel: null,
        nextLevelName: null,
        progress: 100,
        pointsToNext: 0,
        isMaxLevel: true,
      );
    }
    
    // 查找当前等级的起始积分
    final currentLevelConfig = levels.lastWhere(
      (level) => level.minPoints <= points,
      orElse: () => levels.first,
    );
    
    final currentLevelStart = currentLevelConfig.minPoints;
    final nextLevelStart = nextLevelConfig.minPoints;
    
    // 计算进度百分比
    final pointsInCurrentLevel = points - currentLevelStart;
    final pointsNeededForNextLevel = nextLevelStart - currentLevelStart;
    final progress = pointsNeededForNextLevel > 0
        ? ((pointsInCurrentLevel / pointsNeededForNextLevel) * 100).floor().clamp(0, 100)
        : 100;
    final pointsToNext = nextLevelStart - points;
    
    return LevelProgress(
      currentLevel: currentLevel,
      currentLevelName: currentLevelName,
      nextLevel: nextLevelConfig.id - 1,
      nextLevelName: nextLevelConfig.name,
      progress: progress,
      pointsToNext: pointsToNext,
      isMaxLevel: false,
    );
  }
}

/// 等级配置模型
class LevelConfig {
  final int id;
  final String name;
  final String points;
  final int minPoints;

  LevelConfig({
    required this.id,
    required this.name,
    required this.points,
    required this.minPoints,
  });
}

/// 等级进度信息
class LevelProgress {
  final int currentLevel;
  final String currentLevelName;
  final int? nextLevel;
  final String? nextLevelName;
  final int progress;
  final int pointsToNext;
  final bool isMaxLevel;

  LevelProgress({
    required this.currentLevel,
    required this.currentLevelName,
    this.nextLevel,
    this.nextLevelName,
    required this.progress,
    required this.pointsToNext,
    required this.isMaxLevel,
  });
}

